var ClassesList = function () {
    var handleOrders = function() {
        
        var table = $("#datatable_list");
        var grid = new Datatable();
        grid.init({
            src: table,
            onSuccess: function(grid) {
            // execute some code after table records loaded
            },
            onError: function(grid) {
            // execute some code on network or other general error  
            },
            dataTable: {  // here you can define a typical datatable settings from http://datatables.net/usage/options 
                'sAjaxSource' :table.attr('action'),
                "aoColumns": [
                {
                    "mDataProp": "checkbox"
                },
                {
                    "mDataProp": "course"
                },
                {
                    "mDataProp": "team"
                },
                {
                    "mDataProp": "level"
                },
                {
                    "mDataProp": "crew"
                },
                {
                    "mDataProp": "location"
                },
                {
                    "mDataProp": "note"
                },
                {
                    "mDataProp": "actions"
                },
                ],
                "aoColumnDefs" : [
                {
                    'bSortable' : false,
                    'aTargets' : [0,7]
                }
                ],
                "bServerSide": true
            }
        });
        
        // handle filter
        grid.getDataTable().find('.form-filter').change(function(e){
            $('.filter-submit', grid.getDataTable()).trigger('click');
        });
        
        // handle group actionsubmit button click
        grid.getTableWrapper().on('click', '.table-group-action-submit', function(e){
            e.preventDefault();
            var action = $(".table-group-action-input", grid.getTableWrapper());
            if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
                grid.addAjaxParam("sAction", "group_action");
                grid.addAjaxParam("sGroupActionName", action.val());
                var records = grid.getSelectedRows();
                for (var i in records) {
                    grid.addAjaxParam(records[i]["name"], records[i]["value"]);    
                }
                grid.getDataTable().fnDraw();
            } else if (action.val() == "") {
                Metronic.alert({
                    type: 'danger', 
                    icon: 'warning', 
                    message: 'Chưa chọn thao tác xử lý', 
                    container: grid.getTableWrapper(), 
                    place: 'prepend'
                });
            } else if (grid.getSelectedRowsCount() === 0) {
                Metronic.alert({
                    type: 'danger', 
                    icon: 'warning', 
                    message: 'Các mẫu chưa được chọn', 
                    container: grid.getTableWrapper(), 
                    place: 'prepend'
                });
            }
        });

    }
    
    var newClasses = function ($modal) {
        
        $('#btn-new').on('click', function(){
            // create the backdrop and wait for next modal to be triggered
            $('body').modalmanager('loading');
            
            $modal.load($(this).attr('data-href'), '', function(){
                $modal.modal();
            })
            .on('hidden', function () {
                $('.filter-submit').trigger('click');
            });
        });
    }
    
    var ViewUser = function($modal) {
        $( "#datatable_list" ).on( "click", "button.btn-viewuser", function() {
            // create the backdrop and wait for next modal to be triggered
            $('body').modalmanager('loading');
            
            $modal.load($(this).attr('data-href'), '', function(){
                $modal.modal();
            });
        });
    }
    
    var EditClass = function($modal) {
        $( "#datatable_list" ).on( "click", "button.btn-class-edit", function() {
            // create the backdrop and wait for next modal to be triggered
            $('body').modalmanager('loading');
            
            $modal.load($(this).attr('data-href'), '', function(){
                $modal.modal();
            })
            .on('hidden', function () {
                $('.filter-submit').trigger('click');
            });
        });
    }
    
    return {
        
        //main function to initiate the module
        init: function () {
            // general settings
            $.fn.modal.defaults.spinner = $.fn.modalmanager.defaults.spinner = 
            '<div class="loading-spinner" style="width: 200px; margin-left: -100px;">' +
            '<div class="progress progress-striped active">' +
            '<div class="progress-bar" style="width: 100%;"></div>' +
            '</div>' +
            '</div>';
            
            $.fn.modalmanager.defaults.resize = true;
            
            var $modal = $('#ajax-modal');
            
            handleOrders();
            newClasses($modal);
            ViewUser($modal);
            EditClass($modal);
        }
    
    };

}();